package com.pengcheng.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class CtUtil {
    @Autowired
    private Common common;
    public Map<String,Object> decode(String msg)
    {
        Map<String,Object> map=new HashMap<>();
        if(msg.length()!=154)
        {
            return null;
        }
        int address=Integer.parseInt(msg.substring(0,2),16);
        float sn1=common.hexstringtofloat(msg.substring(6,14));
        float sn2=common.hexstringtofloat(msg.substring(14,22));
        String sn=(sn1+"").substring(0,6);
        String str_sn2=(int)sn2+"";
        while(str_sn2.length()<6)
        {
            str_sn2="0"+str_sn2;
        }
        sn=sn+str_sn2;
        float v1=common.hexstringtofloat(msg.substring(22,30));
        float v2=common.hexstringtofloat(msg.substring(30,38));
        float v3=common.hexstringtofloat(msg.substring(38,46));
        float v4=common.hexstringtofloat(msg.substring(46,54));
        float v5=common.hexstringtofloat(msg.substring(54,62));
        float v6=common.hexstringtofloat(msg.substring(62,70));
        float v7=common.hexstringtofloat(msg.substring(70,78));
        float v8=common.hexstringtofloat(msg.substring(78,86));
        float v9=common.hexstringtofloat(msg.substring(86,94));
        float v10=common.hexstringtofloat(msg.substring(94,102));
        int o1=Integer.parseInt(msg.substring(102,106),16);
        int o2=Integer.parseInt(msg.substring(106,110),16);
        int o3=Integer.parseInt(msg.substring(110,114),16);
        int o4=Integer.parseInt(msg.substring(114,120),16);
        int o5=Integer.parseInt(msg.substring(120,124),16);
        int type=Integer.parseInt(msg.substring(124,126),16);
        int year=Integer.parseInt(msg.substring(126,130),16);
        int month=Integer.parseInt(msg.substring(130,134),16);
        int day=Integer.parseInt(msg.substring(134,138),16);
        int hour=Integer.parseInt(msg.substring(138,142),16);
        int minute=Integer.parseInt(msg.substring(142,146),16);
        int second=Integer.parseInt(msg.substring(146,150),16);
        map.put("sn",sn);
        map.put("val1",v1);
        map.put("val2",v2);
        map.put("val3",v3);
        map.put("val4",v4);
        map.put("val5",v5);
        map.put("val6",v6);
        map.put("val7",v7);
        map.put("val8",v8);
        map.put("val9",v9);
        map.put("val10",v10);
        map.put("o1",o1);
        map.put("o2",o2);
        map.put("o3",o3);
        map.put("o4",o4);
        map.put("o5",o5);
        String ddate=year+"-"+(month<10?("0"+month):(""+month))+"-"+(day<10?("0"+day):(""+day))+" "+(hour<10?("0"+hour):(""+hour))+":"+(minute<10?("0"+minute):(""+minute))+":"+(second<10?("0"+second):(""+second));
        map.put("ddate",ddate);
        map.put("fdate",common.format_date(new Date()));
        map.put("deleted","0");
        return map;
    }
}
